import 'package:app/common/colors.dart';
import 'package:app/common/tool.dart';
import 'package:app/components/base_scaffold_appbar.dart';
import 'package:app/components/nodata_page.dart';
import 'package:app/pages/timesheet_module/timesheet_controller.dart';
import 'package:app/pages/timesheet_module/timesheet_model.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swipe_action_cell/core/cell.dart';
import 'package:get/get.dart';

import '../../common/app_theme.dart';
import '../../components/custom_footer.dart';
import '../../utils/hexcolor.dart';

class TimeSheetView extends GetView{


  TimeSheetController controller = Get.put(TimeSheetController());

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: BaseAppBar(
        preferredSize: Size(Get.width, AppTool.appBarHEIGHT),
        action: [
          TextButton(onPressed: ()async{
           var data = await Get.toNamed(AppRoutes.createTimeSheet);
           if(data == 'refresh'){
             controller.requestData();
           }

          }, child: const Text('新建',style: TextStyle(color: Colors.white),))
        ],
        child:const Text('工时单'),
      ),
      body:GetBuilder<TimeSheetController>(builder: (_){
        return Column(
          children: [
            Container(
              height: 50,
              color: Colors.yellowAccent,
              child: BrnSearchText(
                innerPadding: EdgeInsets.only(left: 15,right: 15,top: 6,bottom: 6),
                // focusNode: focusNode,
                controller: controller.searchTextEditingController,
                borderRadius: BorderRadius.all(Radius.circular(20)),
                // searchController: scontroller..isActionShow = true,
                onTextClear: () {
                  return false;
                },
                autoFocus: false,
                onActionTap: () {
                  // scontroller.isClearShow = false;
                  // scontroller.isActionShow = false;
                  // focusNode.unfocus();
                  // BrnToast.show('取消', context);
                },
                onTextCommit: (text) {
                  // BrnToast.show('提交内容 : $text', context);
                },
                onTextChange: (text) {
                  // BrnToast.show('输入内容 : $text', context);
                  controller.requestData(search: text);
                },
              ),
            ),
            Expanded(child:  EasyRefresh.custom(
              footer: MaterialFooter1(),
              onRefresh:()=>controller.onRefresh(),
              onLoad: ()=> controller.onLoad(),
              emptyWidget: controller.dataArr.isEmpty?NoDataPage():null,
              controller: controller.easyRefreshController,
              slivers: [
                SliverList(
                  delegate: _mySliverChildBuilderDelegate(),
                ),
              ],
            ))
          ],
        );
      }),
    );
  }
  _mySliverChildBuilderDelegate() {
    return SliverChildBuilderDelegate(
          (BuildContext context, int index) {
            TimeSheetItemModel model = controller.dataArr[index];
            return SwipeActionCell(
              key: ObjectKey(controller.dataArr[index]),
              trailingActions: <SwipeAction>[
                SwipeAction(
                    title: "删除",
                    onTap: (CompletionHandler handler) async {
                      await handler(true);
                      controller.requestDataWithUnlink(model.id);
                      // list.removeAt(index);
                      // setState(() {});
                    },
                    color: Colors.red),
              ],
              child:GestureDetector(
                onTap: ()async{

                  var data = await Get.toNamed(AppRoutes.timeSheeDetail,arguments: model.id);
                  if(data == 'refresh'){
                    controller.requestData();
                  }
                },
                child: Container(
                  height: 100,
                  padding: EdgeInsets.only(top: 10),
                  child:  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Container(
                        color: Colors.white,
                        padding: EdgeInsets.only(top: 15,left: 15),
                        alignment: Alignment.topLeft,
                        child: Container(
                          width: 40,height: 40,
                          clipBehavior: Clip.hardEdge,
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.all(Radius.circular(20))
                          ),
                          child: CachedNetworkImage(imageUrl: '${Address.homeHost}${model.image}',
                            fit: BoxFit.cover,
                          ),
                        ),
                      ),
                      Expanded(child: Container(
                        width: Get.width-10,
                        color: Colors.white,
                        padding: EdgeInsets.all(10),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              color: Colors.white,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Container(
                                    width: 150,
                                    padding: EdgeInsets.only(left: 0,right: 0),
                                    child: Text('${model.name}',
                                      style: appThemeData.textTheme.bodyText1!.
                                      copyWith(fontWeight: FontWeight.w500,fontSize: 16),maxLines: 1,),
                                  ),
                                  Container(
                                    // decoration: BoxDecoration(
                                    //     color: HexColor('#4369F1').withAlpha(50),
                                    //     borderRadius: BorderRadius.all(Radius.circular(15))
                                    // ),
                                    padding: EdgeInsets.only(left: 5,right: 5,top: 1,bottom: 1),
                                    child: Row(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      children: [
                                        Text('${model.date}'
                                          ,style: appThemeData.textTheme.subtitle1?.copyWith(
                                              fontSize: 11
                                          ),),
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            SizedBox(height: 5,),

                            Container(
                              padding: EdgeInsets.only(left: 0,right: 5),
                              child: Text('项目名称:${model.projectName}',
                                style: appThemeData.textTheme.subtitle1!,),
                            ),

                            Container(
                              padding: EdgeInsets.only(left: 0,right: 5,top: 5),
                              child: Text('任务名称:${model.taskName}',
                                style: appThemeData.textTheme.subtitle1!,),
                            ),

                            // Text('请假类型：${model.typeName!}'),
                            // Text('开始时间：${model.dateStart!}'),
                            // Text('结束时间：${model.dateEnd!}'),
                            // Text('持续时间：${model.duration!}'),
                            // SizedBox(height: 15,),

                          ],
                        ),
                      ),),
                    ],
                  ),
                ),
              ),
            );
      },
      childCount: controller.dataArr.length,
    );
  }


}